import Vue from 'vue'
import Router from 'vue-router'
import Content from './router/content.js'
import Demo from './router/demo.js'
import Wechat from './router/wechat.js'
import System from './router/system.js'
import Member from './router/member.js'
import Shop from './router/shop.js'
import NProgress from '@/plugins/nprogress.js'
import store from './store/'
const whiteList = ['/login', '/register', '/forgetpwd', '/changepwd']
Vue.use(Router)
var routerInstance = new Router({
  routes: [
    { path: '/login', name: 'Login', component: () => import('./views/Login.vue') },
    { path: '/register', name: 'Register', component: () => import('./views/Register.vue') },
    { path: '/forgetpwd', name: 'ForgetPwd', component: () => import('./views/ForgetPwd.vue') },
    { path: '/changepwd', name: 'ChangePwd', component: () => import('./views/ChangePwd.vue') },
    { path: '*', component: () => import('@/views/error/404.vue') },
    { path: '/profile',
      component: () => import('@/apps/admin/Layout/Layout.vue'),
      children: [
        { path: '', name: 'Profile', component: () => import('./views/Profile.vue') }
      ]
    },
    { path: '/', name: 'Home', redirect: { name: 'ContentHome' } },
    Content,
    Demo,
    Wechat,
    Member,
    System,
    Shop
  ],
  scrollBehavior (to, from, savedPosition) {
    // return 期望滚动到哪个的位置
    return { x: 0, y: 0 }
  }
})
routerInstance.beforeEach((to, from, next) => {
  NProgress.start()
  if (whiteList.indexOf(to.path) !== -1) {
    next()
  } else if (store.getters.isLogin && store.getters.token !== undefined) {
    next()
  } else {
    next({ name: 'Login' })
  }
})
routerInstance.afterEach((to, from) => {
  NProgress.done() // finish progress bar
})
export default routerInstance
